"use strict";
module.exports = (sequelize, DataTypes) => {
  const Category = sequelize.define(
    "Category",
    {
      id: {
        type: DataTypes.BIGINT,
        primaryKey: true,
        autoIncrement: true,
      },
      name: DataTypes.STRING,
    },
    {
      tableName: "categories",
      underscored: true,
      timestamps: true,
    }
  );

  Category.associate = function (models) {
    Category.hasMany(models.Goods, {
      foreignKey: "category_id",
      as: "goods",
    });
  };
  if (process.env.SYNC_MODE === "true") {
    Category.sync({ alter: true })
      .then(() => console.log("✅ category同步成功"))
      .catch((err) => console.error("❌ category同步失败:", err));
  }
  return Category;
};
